import * as types from './mutation-types'
import { 
  saveToken,
  saveIsJia,
  removeToken,
  removeIsJia,
  clear
} from 'assets/js/cache'

// 设置token
export const setToken = ({
  commit
}, token) => {
  commit(types.SET_TOKEN, saveToken(token))
}

// 设置isJia
export const setIsJia = ({
  commit
}, isJia) => {
  commit(types.SET_ISJIA, saveIsJia(isJia))
}

// export const loginByUsername = ({commit}, userInfo) => {
//   return new Promise((resolve, reject) => {
//     login(userInfo).then( res => {
//       if(res.success === true) {
//         commit(types.SET_TOKEN, saveToken(res.data.token))
//       }
//       resolve()
//     }).catch(error => {
//       reject(error)
//     })
//   })
// }

// export const loginByUsername = ({commit}, userInfo) => {
//   login(userInfo).then(res => {
//     if(res.success === true) {
//       commit(types.SET_TOKEN, saveToken(res.data.token))
//       // this.$router.push('/home')
//     }
//   })
// }

// 退出
export const logOutAction = ({commit}) => {
  commit(types.SET_TOKEN, '')
  commit(types.SET_ISJIA, '')
  removeToken()
  removeIsJia()
  clear()
}